package com.erp.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.user.domain.ErpBonusPool;
import com.erp.user.domain.ErpBonusPoolInLog;
import com.erp.user.domain.bo.ErpBonusPoolBo;
import com.erp.user.domain.vo.ErpBonusPoolVo;
import com.erp.user.mapper.ErpBonusPoolInLogMapper;
import com.erp.user.mapper.ErpBonusPoolMapper;
import com.erp.user.service.IErpBonusPoolService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * ERP奖金池Service业务层处理
 *
 * @author erp
 * @date 2023-05-11
 */
@RequiredArgsConstructor
@Service
public class ErpBonusPoolServiceImpl implements IErpBonusPoolService {

	private final ErpBonusPoolMapper baseMapper;
	private final ErpBonusPoolInLogMapper erpBonusPoolInLogMapper;

	/**
	 * 查询ERP奖金池
	 */
	@Override
	public ErpBonusPoolVo queryById(Long id) {
		return baseMapper.selectVoById(id);
	}

	/**
	 * 查询ERP奖金池列表
	 */
	@Override
	public TableDataInfo<ErpBonusPoolVo> queryPageList(ErpBonusPoolBo bo, PageQuery pageQuery) {
		LambdaQueryWrapper<ErpBonusPool> lqw = buildQueryWrapper(bo);
		Page<ErpBonusPoolVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
		return TableDataInfo.build(result);
	}

	/**
	 * 查询ERP奖金池列表
	 */
	@Override
	public List<ErpBonusPoolVo> queryList(ErpBonusPoolBo bo) {
		LambdaQueryWrapper<ErpBonusPool> lqw = buildQueryWrapper(bo);
		return baseMapper.selectVoList(lqw);
	}

	private LambdaQueryWrapper<ErpBonusPool> buildQueryWrapper(ErpBonusPoolBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<ErpBonusPool> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getId() != null, ErpBonusPool::getId, bo.getId());
		lqw.like(StringUtils.isNotBlank(bo.getName()), ErpBonusPool::getName, bo.getName());
		lqw.eq(bo.getType() != null, ErpBonusPool::getType, bo.getType());
		lqw.eq(bo.getStartTime() != null, ErpBonusPool::getStartTime, bo.getStartTime());
		lqw.eq(bo.getEndTime() != null, ErpBonusPool::getEndTime, bo.getEndTime());
		lqw.eq(StringUtils.isNotBlank(bo.getWinningTime()), ErpBonusPool::getWinningTime, bo.getWinningTime());
		lqw.eq(bo.getAmount() != null, ErpBonusPool::getAmount, bo.getAmount());
		lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpBonusPool::getRevision, bo.getRevision());
		return lqw;
	}

	/**
	 * 新增ERP奖金池
	 */
	@Override
	public Boolean insertByBo(ErpBonusPoolBo bo) {
		ErpBonusPool add = BeanUtil.toBean(bo, ErpBonusPool.class);
		validEntityBeforeSave(add);
		boolean flag = baseMapper.insert(add) > 0;
		if (flag) {
			bo.setId(add.getId());
		}
		return flag;
	}

	/**
	 * 修改ERP奖金池
	 */
	@Override
	public Boolean updateByBo(ErpBonusPoolBo bo) {
		ErpBonusPool update = BeanUtil.toBean(bo, ErpBonusPool.class);
		validEntityBeforeSave(update);
		return baseMapper.updateById(update) > 0;
	}

	/**
	 * 保存前的数据校验
	 */
	private void validEntityBeforeSave(ErpBonusPool entity) {
		// TODO 做一些数据校验,如唯一约束
	}

	/**
	 * 批量删除ERP奖金池
	 */
	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			// TODO 做一些业务上的校验,判断是否需要校验
		}
		return baseMapper.deleteBatchIds(ids) > 0;
	}

	@Override
	public void save(Long userId, BigDecimal bonusPoolAmount, Integer type) {
		// 根据类型获取最新一期的未开奖的奖金池
		LambdaQueryWrapper<ErpBonusPool> lqw = new LambdaQueryWrapper<>();
		lqw.eq(ErpBonusPool::getType, type);
		lqw.eq(ErpBonusPool::getStatus, 0); // 未开奖
		lqw.last("limit 1");
		ErpBonusPool bonusPool = baseMapper.selectOne(lqw);
		// 存入金额
		BigDecimal amount = bonusPool.getAmount();
		amount = amount.add(bonusPoolAmount);
		bonusPool.setAmount(amount);

		// 修改
		baseMapper.updateById(bonusPool);

		// 添加奖金池存入记录
		ErpBonusPoolInLog inLog = new ErpBonusPoolInLog();
		inLog.setAmount(bonusPoolAmount);
		inLog.setUserId(userId);
		inLog.setPoolId(bonusPool.getId());
		erpBonusPoolInLogMapper.insert(inLog);
	}
}
